package com.wrs.project.module.webrtc

import org.webrtc.CandidatePairChangeEvent
import org.webrtc.DataChannel
import org.webrtc.EglBase
import org.webrtc.IceCandidate
import org.webrtc.MediaStream
import org.webrtc.PeerConnection
import org.webrtc.RtpReceiver
import org.webrtc.RtpTransceiver

object WebRTCHelp {
    fun getEglBase():EglBase {
       val eglBase = EglBase.create()
        return eglBase
    }

    fun getPeerConnectionObserver(
        onSignalingChangeHandle: (signalingState : PeerConnection.SignalingState?)-> Unit,
        onIceConnectionChangeHandle:(iceConnectionState : PeerConnection.IceConnectionState?)-> Unit,
        onIceConnectionReceivingChangeHandle:(flag: Boolean)-> Unit,
        onIceGatheringChangeHandle: (state: PeerConnection.IceGatheringState?)-> Unit,
        onIceCandidateHandle: (ice: IceCandidate?)-> Unit,
        onIceCandidatesRemovedHandle: (data: Array< IceCandidate?>?)-> Unit,
         onSelectedCandidatePairChangedHandle:(event: CandidatePairChangeEvent?)-> Unit,
        onStandardizedIceConnectionChangeHandle:(newState: PeerConnection.IceConnectionState?)-> Unit,
        onAddStreamHandle:(mediaStream : MediaStream?)-> Unit,
        onRemoveStreamHandle:(mediaStream : MediaStream?)-> Unit,
        onDataChannelHandle:(dataChannel : DataChannel?)-> Unit,
        onTrackHandle:(transceiver : RtpTransceiver?) -> Unit,
        onRenegotiationNeededHandle:()-> Unit,
        onAddTrackHandle:(p0: RtpReceiver?, p1: Array< MediaStream?>?)-> Unit,
        onConnectionChangeHandle: (newState: PeerConnection.PeerConnectionState?)-> Unit
    ): PeerConnection.Observer {
        val observer = object : PeerConnection.Observer {
             override fun onConnectionChange(newState: PeerConnection.PeerConnectionState?) {
                 super.onConnectionChange(newState)
                 onConnectionChangeHandle(newState)
             }

            override fun onTrack(transceiver: RtpTransceiver?) {
                super.onTrack(transceiver)
                onTrackHandle(transceiver)
            }

            override fun onSignalingChange(signalingState: PeerConnection.SignalingState?) {
                onSignalingChangeHandle(signalingState)
            }

            override fun onIceConnectionChange(iceConnectionState: PeerConnection.IceConnectionState?) {
                onIceConnectionChangeHandle(iceConnectionState)
            }

            override fun onIceConnectionReceivingChange(flag: Boolean) {
                onIceConnectionReceivingChangeHandle(flag)
            }

            override fun onIceGatheringChange(state: PeerConnection.IceGatheringState?) {
                onIceGatheringChangeHandle(state)
            }

            override fun onIceCandidate(p0: IceCandidate?) {
                onIceCandidateHandle(p0)
            }

             override fun onSelectedCandidatePairChanged(event: CandidatePairChangeEvent?) {
                 super.onSelectedCandidatePairChanged(event)
                 onSelectedCandidatePairChangedHandle(event)
             }

            override fun onIceCandidatesRemoved(p0: Array< IceCandidate?>?) {
                onIceCandidatesRemovedHandle(p0)
            }

            override fun onAddStream(p0: MediaStream?) {
                onAddStreamHandle(p0)
            }

            override fun onRemoveStream(p0: MediaStream?) {
                onRemoveStreamHandle(p0)
            }

            override fun onDataChannel(p0: DataChannel?) {
                onDataChannelHandle(p0)
            }

            override fun onRenegotiationNeeded() {
                onRenegotiationNeededHandle()
            }

            override fun onAddTrack(
                p0: RtpReceiver?,
                p1: Array< MediaStream?>?
            ) {
                onAddTrackHandle(p0, p1)
            }

            override fun onStandardizedIceConnectionChange(newState: PeerConnection.IceConnectionState?) {
                super.onStandardizedIceConnectionChange(newState)
                onStandardizedIceConnectionChangeHandle(newState)
            }

        }

        return observer
    }
}